<?php

namespace App\Http\Controllers;

use App\Http\Service\Douban\DoubanpostService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Http\Service\ViewcountService;
use Illuminate\Support\Facades\DB;

class DoubanpostController extends BaseController
{
    private $getRules=[
            'type'=> 'required',
            //'city_code'=>'required',
        ];
    private  $getRulesMessage=[
            'type.required'=> 'type不能为空',
           // 'city_code.required'=> 'city_code不能为空',

        ];
    public $repository;
    public $historyservice;
    public function __construct(DoubanpostService $repository,ViewcountService $historyservice)
    {
        parent::__construct();
        $this->repository = $repository;
        $this->historyservice = $historyservice;
    }

    


    /**
     *  豆瓣推送消息-Douban_post
     *  
     * @param Request $request
     * @return JsonResponse
     */
    public function addData(Request $request)
    {
        $data=[
            'avatar'=>$request->json("avatar"),
            'article_url'=>$request->json("article_url"),
            'userId'=>$request->json("userId"),
            'userName'=>$request->json("userName"),
            'publicTime'=>$request->json("publicTime"),
            'title'=>$request->json("title"),
            'content'=>$request->json("content"),
            'replys'=>$request->json("replys"),
            'photos_url'=>$request->json("photos_url"),
            'unique_id'=>$request->json("unique_id"),
            'extractTime'=>$request->json("extractTime"),
            'push_task_id'=>$request->json("push_task_id"),
            'channel'=>'豆瓣',
            'group_name'=>$request->json("group_name"),

        ];

        if(empty($data['replys'])){
            $data['replys']=[];
        }
        if(empty($data['content'])){
            $data['content']=[];
        }
        if(empty($data['photos_url'])){
            $data['photos_url']=[];
        }
        //die("sdfsd");
        $data['replys'] = json_encode($data['replys'], JSON_UNESCAPED_UNICODE);
        $data['content'] = json_encode($data['content'], JSON_UNESCAPED_UNICODE);
        $data['photos_url'] = json_encode($data['photos_url'], JSON_UNESCAPED_UNICODE);
        
        $this->success($this->repository->addData($request,$data));
    }


    //app用户-添加帖子
    public function addAppData(Request $request){
        $data=[
            'avatar'=>$request->json("avatar"),
            'userId'=>$request->json("userId"),
            'userName'=>$request->json("userName"),
            'publicTime'=>date("Y-m-d H:i:s"),
            'title'=>$request->json("title"),
            'content'=>$request->json("content"),
            'photos_url'=>$request->json("photos_url"),
            'guanlian_shop'=>$request->json("guanlian_shop"),
        ];
        #处理关联商品
        if(empty($data['guanlian_shop'])){
            $data['guanlian_shop']=[];

        }
        if($data['guanlian_shop']=='[]'){
            $data['guanlian_shop']=[];
        }
        if($data['guanlian_shop']){
            foreach ($data['guanlian_shop'] as  &$v) {
                $v['create_time']=date("Y-m-d H:i:s");
            }
        }
        #处理回复内容
        if($data['content']=='[]'){
            $data['content']=[];
        }
        if(empty($data['content'])){
            $data['content']=[];
        }
        #处理图片内容
        if($data['photos_url']=='[]'){
            $data['photos_url']=[];
        }

        if(empty($data['photos_url'])){
            $data['photos_url']=[];
        }

        //dd($data['guanlian_shop']);
        $data['content'] = json_encode($data['content'], JSON_UNESCAPED_UNICODE);
        $data['guanlian_shop'] = json_encode($data['guanlian_shop'], JSON_UNESCAPED_UNICODE);
        $data['photos_url'] = json_encode($data['photos_url'], JSON_UNESCAPED_UNICODE);

        $this->success($this->repository->addAppData($request,$data));

    }
    //app用户-修改帖子
    public function editAppData(Request $request){
        $data=[
            'id'=>$request->json("id"),
            'avatar'=>$request->json("avatar"),
            'userId'=>$request->json("userId"),
            'userName'=>$request->json("userName"),
            'publicTime'=>date("Y-m-d H:i:s"),
            'title'=>$request->json("title"),
            'content'=>$request->json("content"),
            'photos_url'=>$request->json("photos_url"),
            'guanlian_shop'=>$request->json("guanlian_shop"),
        ];
        if(empty($data['id'])){
            $this->error("帖子id不能为空");
        }
        $data['content'] = json_encode($data['content'], JSON_UNESCAPED_UNICODE);
        $data['guanlian_shop'] = json_encode($data['guanlian_shop'], JSON_UNESCAPED_UNICODE);
        $data['photos_url'] = json_encode($data['photos_url'], JSON_UNESCAPED_UNICODE);

        $this->success($this->repository->editAppData($request,$data));

    }
    //app用户-帖子详情
    public function appDatadetail(Request $request){
        $data=[
            'id'=>$request->json("id"),

        ];
        //dd($data['id']);
        if(empty($data['id'])){
            $this->error("帖子id不能为空");
        }

        $this->success($this->repository->appDatadetail($request,$data));

    }
    //app用户-帖子列表
    public function getListAppData(Request $request){
        $data=[
            'user_id'=>$request->json("userId"),
            'page'=>$request->json("page"),
            'limit'=>$request->json("limit"),
        ];

        $this->success($this->repository->getListAppData($request,$data));


    }
    //app用户-帖子删除
    public function appDatadel(Request $request){
        $ids = $request->json('id');
        if(empty($ids)){
            $this->error('参数错误');
        }
        $data=[
        
        ];
        $this->repository->appDatadel($request,$data,$ids);


    }
    //管理员删除-帖子
    public function officialDatadel(Request $request){
        $ids = $request->json('id');
        if(empty($ids)){
            $this->error('参数错误');
        }
        $data=[
            'content'=>$request->json('content'),
            'ids'=>$ids,
            'user_id'=>$request->json('user_id'),
        
        ];
       // dd($data);
        $this->repository->officialDatadel($request,$data,$ids);


    }

/*    //帖子违规-通知

    public function cardMsgList(Request $request)
    {

        $data=[
            'people_id'=>$request->json("people_id"),
            'limit'=>$request->json("limit"),
            'page'=>$request->json("page"),
            'type'=>$request->json("type"),
        ];
        $this->success($this->repository->cardMsgList($request,$data));

    }*/
  

}
